package com.liqi.common.core.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

/**
 * 返回结果包装类
 *
 * @author tianye
 * @date 2020-12-26
 */
@ApiModel("统一返回结果")
public class R<T> implements Serializable {

    /**
     * 200-成功
     */
    @ApiModelProperty("状态码：200-成功，其他失败，默认-1失败")
    private Integer code=200;

    // 响应描述
    @ApiModelProperty("描述")
    private String msg;

    // 响应数据
    @ApiModelProperty("返回数据")
    private T data;

    public static R ok() {
        R r = new R();
        r.setCode(200);
        r.setMsg("成功");
        return r;
    }
    public static <T> R ok(String msg,T data) {
        R r = ok();
        r.setData(data);
        r.setMsg(msg);
        return r;
    }

    public static <T> R ok(T data) {
        R r = ok();
        r.setData(data);
        return r;
    }

    public static R fail(String message) {
        R r = new R();
        r.setCode(-1);
        r.setMsg(message);
        return r;
    }

    public static <T> R fail(String message, T data) {
        R r = new R();
        r.setCode(-1);
        r.setMsg(message);
        r.setData(data);
        return r;
    }
    
    public static <T> R fail(int code,String message, T data) {
        R r = new R();
        r.setCode(code);
        r.setMsg(message);
        r.setData(data);
        return r;
    }

    @JsonIgnore
    public boolean isSuccess() {
        if (code == null) {
            throw new RuntimeException("未知状态");
        }
        return code == 200;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
